package club.yuit.desktop.ui;

import com.formdev.flatlaf.extras.FlatSVGIcon;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

/**
 * @author: yuit
 * @date: 2023/12/12 19:20
 */
public class UITools {
    /**
     * 查找当前组件的父组件并关闭
     * @param c 当前组件
     */
    public static void disposeParent(JComponent c) {
        Window windowAncestor = SwingUtilities.getWindowAncestor(c);
        if (windowAncestor!= null) {
            windowAncestor.dispose();
        }
    }


    public static ImageIcon getIcon(String name){
        URL resource = UITools.class.getResource(name);
        if(resource!=null){
            return new ImageIcon(resource);
        }else{
            return null;
        }
    }


    public static ImageIcon getIcon(String name,double scale){
        URL resource = UITools.class.getResource(name);
        if(resource!=null){
            ImageIcon icon = new ImageIcon(resource);
            int w = (int) (icon.getIconWidth()*scale);
            int h= (int) (icon.getIconHeight()*scale);
            return new ImageIcon(icon.getImage().getScaledInstance(w,h,Image.SCALE_SMOOTH));
        }else{
            return null;
        }
    }

    public static FlatSVGIcon getSVGIcon(String name){
        URL resource = UITools.class.getResource(name);
        if(resource!=null){
           return new FlatSVGIcon(resource);
        }else{
            return null;
        }
    }

    public static FlatSVGIcon getSVGIcon(String name,int w,int h){
        FlatSVGIcon svgIcon = getSVGIcon(name);
        if(svgIcon!=null){
            return svgIcon.derive(w,h);
        }else{
            return null;
        }
    }

}
